home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 151-175 / scopedisk168 / clianywhere / screenwatch.c < prev    next >
C/C++ Source or Header  |  1995-03-19  |  4KB  |  182 lines

  1. ;/*
  2. FailAt 1
  3. LC -M -v -iINCLUDE:CompactH/ ScreenWatch.c
  4. Blink DEFINE __main=__tinymain FROM lib:cback.o ScreenWatch.o LIB lib:lc.lib SC SD ND
  5. Quit
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <intuition/intuition.h>
  11. #include <exec/tasks.h>
  12. #include <libraries/dos.h>
  13. #include <proto/exec.h>
  14. #include <proto/intuition.h>
  15.  
  16. /*
  17. **  externs for cback.o
  18. */
  19. LONG _stack         = 4000L;
  20. char *_procname     = "ScreenWatch_v1.0";
  21. LONG _priority      = 0L;
  22. LONG _BackGroundIO  = 0L;
  23.  
  24. struct IntuitionBase *IntuitionBase = NULL;
  25.  
  26. #define SLEEP_SIG SIGBREAKF_CTRL_F
  27.  
  28. struct TextAttr Topaz80 = {"topaz.font",TOPAZ_EIGHTY,FS_NORMAL,FPF_ROMFONT};
  29.  
  30. int req(char *message, char *no_button, char *yes_button,
  31.           struct Window *window)
  32. {
  33.   register struct Window *w;
  34.   register short width;
  35.   register short choice;
  36.   register struct IntuiText *ack;
  37.   register struct IntuiMessage event;
  38.   register struct Message *msg;
  39.   struct IntuiText req_body = {0,1,JAM2,8,4,&Topaz80,NULL,NULL};
  40.   struct IntuiText req_ack = {AUTOFRONTPEN,AUTOBACKPEN,AUTODRAWMODE,
  41.                               AUTOLEFTEDGE,AUTOTOPEDGE,&Topaz80,
  42.                               NULL,AUTONEXTTEXT};
  43.   struct IntuiText req_nak = {AUTOFRONTPEN,AUTOBACKPEN,AUTODRAWMODE,
  44.                               AUTOLEFTEDGE,AUTOTOPEDGE,&Topaz80,
  45.                               NULL,AUTONEXTTEXT};
  46.  
  47.   req_body.IText = message;
  48.   width = ( IntuiTextLength(&req_body) + 39 );
  49.  
  50.   req_nak.IText = no_button;
  51.  
  52.   if (yes_button)
  53.     {
  54.       ack = &req_ack;
  55.       req_ack.IText = yes_button;
  56.     }
  57.   else
  58.     {
  59.       ack = NULL;
  60.     }
  61.  
  62.   w = BuildSysRequest(window, &req_body, ack, &req_nak,
  63.                       (VANILLAKEY|GADGETUP), width, 47);
  64.   if ((ULONG)w < 2L) return((int)w);  /* an alert was used */
  65.  
  66.   choice = -1;
  67.   do
  68.     {
  69.       WaitPort(w->UserPort);
  70.       msg = GetMsg(w->UserPort);
  71.       memcpy( (char *)&event, (char *)msg, sizeof(struct IntuiMessage));
  72.       ReplyMsg(msg);
  73.  
  74.       switch(event.Class)
  75.         {
  76.           case GADGETUP:
  77.             {
  78.               choice = ((struct Gadget *)event.IAddress)->GadgetID;
  79.               break;
  80.             }
  81.           case VANILLAKEY:
  82.             {
  83.               switch(event.Code)
  84.                 {
  85.                   case 27:  /* Esc */
  86.                   case 'N': /* "N" key */
  87.                   case 'n': /* "n" key */
  88.                     {
  89.                       choice = 0;
  90.                       break;
  91.                     }
  92.                   case 13:  /* Return */
  93.                   case 'Y': /* "Y" key */
  94.                   case 'y': /* "y" key */
  95.                     {
  96.                       choice = 1;
  97.                       break;
  98.                     }
  99.                 }
  100.               break;
  101.             }
  102.         }
  103.     }
  104.   while(choice == -1);
  105.  
  106.   FreeSysRequest(w);
  107.  
  108.   return((int)choice);
  109. }
  110.  
  111. #define msgreq(t,w) req((t),"Okay",NULL,(w))
  112. #define askreq(t,w) req((t),"No!","Yes",(w))
  113.  
  114.  
  115. void (* __asm old_CloseScreen)(register __a0 struct Screen *) = NULL;
  116.  
  117.  
  118. UBYTE use_cnt = 0;
  119.  
  120.  
  121. void __asm __saveds new_CloseScreen(register __a0 struct Screen *s)
  122. {
  123.   UBYTE rep = 0;
  124.   use_cnt++;
  125.  
  126.   while(s->FirstWindow != NULL)
  127.     {
  128.       rep++;
  129.       if (rep == 1) ScreenToFront(s);
  130.       if (!req("A window is open on this screen!","Ignore It",
  131.                   "I've Closed It",s->FirstWindow)) break;
  132.     }
  133.  
  134.   old_CloseScreen(s);
  135.   use_cnt--;
  136. }
  137.  
  138.  
  139. int CXBRK(void)
  140. {
  141.   return(0);
  142. }
  143.  
  144.  
  145. main()
  146. {
  147.   struct Task *t, *me;
  148.  
  149. /*
  150. **  determine if there is already a copy of this program running
  151. */
  152.   me = FindTask(NULL);
  153.   me->tc_Node.ln_Name[0] = '@'; /* don't want to find ourselves again */
  154.   t = FindTask(_procname);
  155.   me->tc_Node.ln_Name[0] = *_procname;
  156.   if (t)
  157.     {
  158.       Signal(t,SLEEP_SIG);
  159.       exit(0);
  160.     }
  161.  
  162.   IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",33L);
  163.   if (!IntuitionBase) exit(0);
  164.  
  165.   Forbid();
  166.   old_CloseScreen = SetFunction(IntuitionBase,-0x0042,new_CloseScreen);
  167.   Permit();
  168.  
  169.   SetSignal(0L,SLEEP_SIG);
  170.   Wait(SLEEP_SIG);    /* wait until we get asked to leave */
  171.   while(use_cnt > 0); /* can't pull up the rug while someone's standing on it */
  172.  
  173.   Forbid();
  174.   SetFunction(IntuitionBase,-0x0042,old_CloseScreen);
  175.   Permit();
  176.  
  177.   WBenchToFront();
  178.   msgreq("ScreenWatch removed",NULL);
  179.  
  180.   CloseLibrary((struct Library *)IntuitionBase);
  181. }
  182.